<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <title>公交线路查询</title>
  <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
  <script type="text/javascript"
          src="http://webapi.amap.com/maps?v=1.4.3&key=0a6a2ea94f6a2641e05ffd173371ec2b&plugin=AMap.LineSearch"></script>
  <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
  <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/jquery/jquery.js"></script>
</head>
<body>
<div id="container"></div>
<script language="javascript">
  /*
   * 该示例主要流程分为三个步骤
   * 1. 首先调用公交路线查询服务(lineSearch)
   * 2. 根据返回结果解析，输出解析结果(lineSearch_Callback)
   * 3. 在地图上绘制公交线路()
   */
  var map = new AMap.Map("container", {
    resizeEnable: true,
    center: [114.298572, 30.584355],//地图中心点
    zoom: 13 //地图显示的缩放级别
  });
  /*公交线路查询*/
  function lineSearch() {
    //实例化公交线路查询类，只取回一条路线
    var linesearch = new AMap.LineSearch({
      pageIndex: 1,
      city: '武汉',
      pageSize: 1,
      extensions: 'all'
    });
    //搜索“10”相关公交线路
      let data = ["560","541","602","238","379","385","348","724","906","10","359","215","503","216","568","98","528","246","512","555","20","329","760","601","622","922","369","339","393","575","658","780","61","788","402","269","920","72","240","632","293","706","637","530","656","376","119","341","619","717","85","395","638","340","566","363","259","571","266","919","905","76","737","321","322","755","364","236","620","793","276","561","510","713","509","249","715","739","324","553","726","758","343","377","86","382","564","118","99","294","59","546","708","372","535","712","116","578","4","740","605","97","241","515","356","615","366","371","351","547","301","616","251","621","333","290","271","383","292","527","704","649","298","524","916","791","268","380","26","610","759","93","507","16","265","654","607","229","291","212","523","30","795","394","332","732","577","207","286","581","729","38","907","720","233","89","539","722","213","549","506","711","346","787","542","84","367","231","750","511","296","308","521","583","40","902","7","625","370","353","306","617","392","295","15","538","349","735","208","558","368","390","378","53","608","407","413","24","634","543","903","730","323","334","593","618","741","405","248","315","917","79","738","709","613","360","586","554","87","391","287","342","252","289","710","525","318","74","23","589","552","90","598","603","35","628","336","585","657","328","243","588","727","786","580","91","46","205","723","335","627","646","526","253","508","582","502","88","107","909","636","1","592","399","777","387","350","316","311","272","81","913","551","716","513","587","705","796","591","362","355","629","612","273","702","769","734","352","94","228","117","522","532","270","82","60","915","373","556","785","317","545","267","736","319","313","572","309","34","531","753","540","728","548","96","533","230","312","756","330","653","401","594","389","721","47","703","347","345","751","260","643","797","718","782","516","235","792","584","245","518","559","520","92","262","71","596","327","232","576","310","9","550","201","908","325","14","725","326","790","361","365","239","910","381","517","261","707","242","623","247","606","573","386","536","25","701","299","597","611","18","570","901","42","562","217","83","307","66","202","534","102","106","789","590","733","514","211","757","912","396","297","579","358","563","411","918"]
      let lines = []
      for (let i = 0; i < data.length; i++) {
        linesearch.search(data[i], function(status, result) {
          if (status === 'complete' && result.info === 'OK') {
            lines.push(lineSearch_Callback(result))
            if (i === data.length - 1 ) {
              console.log(JSON.stringify(lines))
            }
          } else {
//            alert(result);
          }
        })
      }

    /*linesearch.search(null, function(status, result) {
      if (status === 'complete' && result.info === 'OK') {
//        lineSearch_Callback(result);
        console.log(result)
      } else {
        alert(result);
      }
    });*/
  }
  /*公交路线查询服务返回数据解析概况*/
  function lineSearch_Callback(data) {
    var lineArr = data.lineInfo;
/*    var lineNum = data.lineInfo.length;
    if (lineNum === 1) {
      let lnglats = lineArr[0].path
      let arr = lnglats.map((item) => {
        return [item.lng, item.lat]
      })
      return arr
    } else {
      console.log('问题数据' + data.lineInfo)
    }*/

    var lineNum = data.lineInfo.length;
    if (lineNum == 0) {
    } else {
      for (var i = 0; i < lineNum; i++) {
        var pathArr = lineArr[i].path;
        var stops = lineArr[i].via_stops;
        var startPot = stops[0].location;
        var endPot = stops[stops.length - 1].location;

        if (i == 0) drawbusLine(startPot, endPot, pathArr);
      }
    }
  }
  /*绘制路线*/
  function drawbusLine(startPot, endPot, BusArr) {
    //绘制起点，终点
    new AMap.Marker({
      map: map,
      position: [startPot.lng, startPot.lat], //基点位置
      icon: "http://webapi.amap.com/theme/v1.3/markers/n/start.png",
      zIndex: 10
    });
    new AMap.Marker({
      map: map,
      position: [endPot.lng, endPot.lat], //基点位置
      icon: "http://webapi.amap.com/theme/v1.3/markers/n/end.png",
      zIndex: 10
    });
    //绘制乘车的路线
    busPolyline = new AMap.Polyline({
      map: map,
      path: BusArr,
      strokeColor: "#09f",//线颜色
      strokeOpacity: 0.8,//线透明度
      strokeWeight: 6//线宽
    });
    map.setFitView();
  }
  lineSearch();
</script>
</body>
</html>
